package com.mall.sso.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.mall.manager.pojo.TbUser;
import com.mall.pojo.TaotaoResult;
import com.mall.sso.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


@Service
@Component
@Transactional
public class TokenServiceImp implements TokenService {
    
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Value("${session_Key}")
    private String session_Key;

    
    @Override
    public TaotaoResult verifyToken(String token) {
        if (token == null) {
            return null;
        }
        String key = session_Key + token;
        String userJsonString = stringRedisTemplate.opsForValue().get(key);
        if (userJsonString != null && !userJsonString.equals("")){
            TbUser user = JSONObject.parseObject(userJsonString, TbUser.class);
            return TaotaoResult.ok(user);
        }else {
            return TaotaoResult.build(400,"登录过期请重新登录");
        }
    }
}
